﻿
using ClientDemo.Models;
using Newtonsoft.Json;
using System;
using System.Net.Http;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace ClientDemo.Services
{
    /// <summary>
    /// WEBAPI服务类
    /// </summary>
    public class WebManager
    {
        private static string url = "http://localhost:5116/Manager/recipeSelectRequest";
        
        public static async Task<T> RecipeSelectRequestAsync<T>(RecipeSelectRequest entity)
        {
            try
            {
                using (HttpClient client = new HttpClient())
                {
                    var content = new StringContent(JsonConvert.SerializeObject(entity), Encoding.UTF8, "application/json");
                    var response = await client.PostAsync(url, content);
                    if (response.IsSuccessStatusCode)
                    {
                        string json = await response.Content.ReadAsStringAsync();
                        return JsonConvert.DeserializeObject<T>(json);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return default(T);
        }
    }
}
